define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {
    if(Config.accounturl == '' || Config.accounturl == null){
        Config.accounturl = 0;
    }
    if(Config.custom_status == '' || Config.custom_status == null){
        Config.custom_status = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionordershoplaza/index/?accounturl=' + Config.accounturl + '&custom_status=' + Config.custom_status,
                    add_url: 'feditionordershoplaza/add',
                    edit_url: 'feditionordershoplaza/edit',
                    del_url: 'feditionordershoplaza/del',
                    multi_url: 'feditionordershoplaza/multi',
                    import_url: 'feditionordershoplaza/import',
                    table: 'fedition_ordershoplaza',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updated_at',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('订单ID'), operate: 'LIKE'},
                        //{field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'accounturl', title: __('店铺名称'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        {field: 'accounturl_name', title: __('店铺名称'), operate: false},
                        {field: 'accounturl_url', title: __('店铺链接'), operate: false},
                        {field: 'admin_name', title: __('店铺管理者'), operate: false},
                        {field: 'created_at', title: __('Created_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updated_at', title: __('Updated_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'ytotal_price', title: __('原价'), operate: 'LIKE'},
                        {field: 'total_price', title: __('当前价格'), operate: 'LIKE'},
                        //{field: 'currency', title: __('Currency'), operate: 'LIKE'},
                        //{field: 'tracking_company', title: __('运货公司'), operate: false},
                        {field: 'tracking_number', title: __('运单号'), operate: false},
                        {field: 'transaction_no', title: __('支付订单号'), operate: 'LIKE'},
                        {field: 'quantityp_title', title: __('部分商品名称'), operate: 'LIKE'},
                        {field: 'email', title: __('邮箱'), operate: 'LIKE'},
                        {field: 'shipping_name', title: __('购买人姓名'), operate: 'LIKE'},
                        {field: 'phone', title: __('联系方式'), operate: 'LIKE'},

                        {field: 'freightmj', title: __('运费成本（美金）'), operate: 'BETWEEN'},
                        // {field: 'gross_profit', title: __('毛利（美金）'), operate: 'BETWEEN'},
                        {field: 'istransfer', title: __('运费成本状态'), searchList: {"0":__('未对接'),"1":__('对接成功'),"3":__('对接接口无数据'),"5":__('手动添加完成')}, formatter: Table.api.formatter.status,custom: {3:'danger', 1:'success', 0:'warning', 5:'info'}},
                        {field: 'material', title: __('采购成本'), editable: false, operate: 'BETWEEN'},
                        {field: 'ismaterial', title: __('采购成本状态'), searchList: {"0":__('未对接'),"1":__('对接成功'),"3":__('对接接口无数据'),"4":__('手动添加完成')}, formatter: Table.api.formatter.status,custom: {3:'danger', 1:'success', 0:'warning', 4:'info'}},

                        //详情点进去
                        //{field: 'city', title: __('城市'), operate: 'LIKE'},
                        //{field: 'address1', title: __('详细地址'), operate: 'LIKE'},
                        //{field: 'gateway', title: __('支付方式'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'none':'info','online':'warning','cod':'info'}},
                        {field: 'order_status', title: __('订单状态'), searchList: {"opened":__('未完成'),"placed":__('进行中'),"finished":__('已完成'),"cancelled":__('取消')}, formatter: Table.api.formatter.status,custom: {cancelled:'danger', finished:'success', opened:'warning', placed:'warning'}},
                        {field: 'financial_status', title: __('Financial_status'), searchList: {"paid":__('已支付'),"refunded":__('已退款'),"partially_refunded":__('部分退款'),"paying":__('正在支付'),"failed":__('失败'),"waiting":__('待付款'),"refunding":__('退款中'),"cancelled":__('取消'),"refund_failed":__('退款失败')}, formatter: Table.api.formatter.status,custom: {refunded:'danger', paid:'success', partially_refunded:'warning', cancelled:'warning', refunding:'warning', refund_failed:'danger'}},
                        {field: 'fulfillment_status', title: __('发货状态'), searchList: {"finished":__('已发货'),"partially_finished":__('部分发货'),"cancelled":__('取消'),"returning":__('退货'),"partially_shipped":__('部分发货'),"returned":__('退回'),"shipped":__('装货'),"initialled":__('初始'),"waiting":__('待发货')}, formatter: Table.api.formatter.status,custom: {returning:'danger', finished:'success', partially_shipped:'warning', shipped:'warning', cancelled:'warning'}},
                        {field: 'refund_status', title: __('售后状态'), searchList: {"none":__('售后待处理'),"general":__('部分退款'),"customize":__('售后处理中'),"":__('暂无')}, formatter: Table.api.formatter.status,custom: {customize:'danger', none:'success', general:'warning'}},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('订单详情'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看商品详情'),
                                    title: __('查看商品详情'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-info btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionorderquantityp/index'
                                },
                                {
                                    name: 'detail',
                                    text: __('查看物流详情'),
                                    title: __('查看物流详情'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionorderfulfillmentsp/index'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('订单采购成本'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('更新采购成本'),
                                    title: __('更新采购成本'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershoplaza/addmaterial',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('更新订单'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('获取订单最新数据'),
                                    title: __('获取订单最新数据'),
                                    classname: 'btn btn-sm btn-info btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershoplaza/updateordertime',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('订单操作'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('取消订单'),
                                    title: __('取消订单'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    confirm: '确认要取消订单吗？',
                                    url: 'feditionordershoplaza/cancel_order',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('订单退款'),
                                    title: __('订单退款'),
                                    classname: 'btn btn-sm btn-info btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认要订单退款吗？',
                                    url: 'feditionordershoplaza/refund_order',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                    ]
                ]
            });
            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#zsum").text(data.Statistics.zsum);
                $("#zcountnum").text(data.Statistics.zcountnum);
                $("#ytotalnum").text(data.Statistics.ytotalnum);
            });
            //联动
            $("#accounturl_type").change(function(){
                var info = $(this).children('option:selected').val();
                $('#js_accounturl_type').val(info);
            });
            $("#accounturl_status").change(function(){
                var info = $(this).children('option:selected').val();
                $('#js_accounturl_status').val(info);
            });
            $("#accounturl_text").change(function(){
                var info = $('#accounturl_text').val();
                $('#accounturl').val(info);
            });
            $("#accounturl_text").data("params", function(){
                var type = $('#js_accounturl_type').val();
                var status = $('#js_accounturl_status').val();
                if(status == ''){
                    if(type == 'shopify'){
                        return {custom: {iscustom: 1}};
                    }
                    if(type == 'Shoplaza'){
                        return {custom: {iscustom_shoplaza: 1}};
                    }
                }else{
                    if(type == 'shopify'){
                        return {custom: {iscustom: 1,status: status}};
                    }
                    if(type == 'Shoplaza'){
                        return {custom: {iscustom_shoplaza: 1,status: status}};
                    }
                }

            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});